package qqclient.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 该类/对象，提供和消息相关的服务方法
 */
public class MessageClientService {

    /**
     * @param content  内容
     * @param senderId 发送者id
     * @param getterId 接收者id
     */
    //私聊
    public void sendMessageToOne(String content, String senderId, String getterId) {
        //构建Message对象
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_COMM_MES);//普通聊天消息
        message.setContent(content);
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setSendTime(new Date().toString());//发送时间设置到message对象
        System.out.println(message.getSendTime() + "\n" +
                "你对" + getterId + "说: " + content);
        //发送给服务端
        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    ManageClientConnectServerThread.
                            getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param contentAll 群聊内容
     * @param senderId   发送者
     */
    //群聊
    public void sendMessageToAll(String contentAll, String senderId) {
        //构建Message对象
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);//群聊消息
        message.setContent(contentAll);
        message.setSender(senderId);
        message.setSendTime(new Date().toString());//发送时间设置到message对象
        System.out.println(message.getSendTime() + "\n" + "你对大家说: " + contentAll);
        //发送给服务端
        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    ManageClientConnectServerThread.
                            getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
